package com.apexedu.portal.service.impl;

import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.portal.action.PortalLogoAction;
import com.apexedu.util.FileUtil;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by janabo on 2014-10-11.
 */

@Service("portalLogoService")
public class PortalLogoServiceImpl extends AbstractBaseServiceSupport {

    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        PortalLogoAction action = (PortalLogoAction) myaction;
        fileUpload(action);      //附件上传
    }

    @Override
    public void load(Object o) throws Exception {

    }

    @SuppressWarnings("unchecked")
    @Override
    public List<String> list(Object myaction, Pager pager) throws Exception {
//        PortalLogoAction action = (PortalLogoAction) myaction;
        List<String> listIcon = new ArrayList<String>();
        String savePath = ServletActionContext.getRequest().getRealPath("/upload");
        File file = new File(savePath);
        for (File f : file.listFiles()) {
            listIcon.add(f.getName());
        }
        return listIcon;
    }

    @Override
    public boolean remove(Object o) throws Exception {
        return false;
    }


    /**
     * 文件上传
     * @param action
     * @return
     * @throws Exception
     */
    private void fileUpload(PortalLogoAction action) throws Exception {
        String savePath = ServletActionContext.getRequest().getRealPath("/upload");
        File logo = action.getLogo();   //上传的文件
        String filename = action.getLogoFileName();     //文件原名
        String fileSaveName = null;     //文件保存名

        //设置允许上传的文件类型
        List<String> allowExtList = new ArrayList<String>();
        allowExtList.add("jpg");
        allowExtList.add("png");
        allowExtList.add("gif");
        if (logo != null && filename != null) {
            String fileExtName = FileUtil.getFileExt(filename); //扩展名
            if (!allowExtList.contains(fileExtName.toLowerCase())) {
                action.addActionError("保存失败！只允许上传" + allowExtList + "类型的图片");
                throw new RuntimeException("只允许上传 " + allowExtList + "类型的图片！");
            }
            //保存文件的名称
            fileSaveName = SeqFactory.getNewSequenceAlone() + ".jpg";
            File savefile = new File(new File(savePath), fileSaveName);
            if (!savefile.getParentFile().exists())
                try {
                    savefile.getParentFile().mkdirs();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            FileUtils.copyFile(logo, savefile);
        } else {
            //新增没有上传文件给出提示
            filename = "";
            fileSaveName = "";
        }
    }
}
